//
//  LGDeviceManageTableCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/2/24.
//

#import "LGDeviceManageTableCell.h"

@interface LGDeviceManageTableCell()
/**   */
@property (nonatomic , strong)UILabel *patientLabel;
/**   */
@property (nonatomic , strong)UILabel *phoneLabel;
/**   */
@property (nonatomic , strong)UIView *backView;
/**   */
@property (nonatomic , strong)UILabel *nameTitle;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UILabel *typeTitle;
/**   */
@property (nonatomic , strong)UILabel *orderStatus;
/**   */
@property (nonatomic , strong)UILabel *deviceTitle;
/**   */
@property (nonatomic , strong)UILabel *deviceStatus;
/**   */
@property (nonatomic , strong)UIButton *actionBtn;
/**   */
@property (nonatomic , strong)UIButton *detailBtn;
/**   */
@property (nonatomic , strong)NSDictionary *orderColor;
/**   */
@property (nonatomic , strong)NSDictionary *deviceColor;

@end

@implementation LGDeviceManageTableCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.orderColor = @{@"生效中":@"#4C87FD",@"未生效":@"#4C87FD",@"未退款":@"#4C87FD",@"已到期":@"#909399",@"已退款":@"#909399"};
        self.deviceColor = @{@"0":@"#4C87FD",@"3":@"#4C87FD",@"10":@"#4C87FD",@"20":@"#4C87FD",@"40":@"#4C87FD",@"103":@"#4C87FD",@"30":@"#909399",@"50":@"#909399",@"102":@"#909399",@"104":@"#909399",@"2":@"#909399",@"9":@"#909399",@"101":@"#909399"};
        [self createSubView];
    }
    return  self;
}

//服务包的
-(void)setServiceDic:(NSDictionary *)serviceDic{
    _serviceDic = serviceDic;
    if (!serviceDic || serviceDic.allKeys.count == 0) return;
    self.nameTitle.text = LGNSString(serviceDic[@"give_type_name"]);
    self.typeTitle.text = @"服  务  包";
    self.nameLabel.text = LGNSString(serviceDic[@"title"]);
    self.patientLabel.text = [NSString stringWithFormat:@"%@  %@",serviceDic[@"realname"],serviceDic[@"mobile"]];
    //state:10-待领取 、20-待发货 、 30-已发货 、 40-已寄出 、 50-已入库 、 101-已取消 、 102-已赠送（续包赠送） 、 103-待归还 、104-回收失败、105-续包赠送
    NSString *state = LGNSString(serviceDic[@"state"]);
    NSString *serviceState = LGNSString(serviceDic[@"vip_status_name"]);
    //3-设备赠送 、 4-设备租用 、 8-设备租用[需押金] 、 6-长程心电监测到家服务
    NSString *vipService = LGNSString(serviceDic[@"vip_service_type"]);

    self.orderStatus.text = serviceState;
    self.orderStatus.textColor = [self.orderColor.allKeys containsObject:serviceState]?[UIColor colorWithString:self.orderColor[serviceState]]:[UIColor colorWithString:@"#909399"];
    
    self.deviceStatus.text = LGNSString(serviceDic[@"state_name"]);
    self.deviceStatus.textColor = [self.deviceColor.allKeys containsObject:state]?[UIColor colorWithString:self.deviceColor[state]]:[UIColor colorWithString:@"#909399"];
    
    if ([vipService isEqualToString:@"3"] && [state isEqualToString:@"103"] && [serviceState  isEqualToString:@"已退款"] ) {
        //设备赠送类型下 -- 已退款+待回收 才显示设备回收按钮
        self.actionBtn.hidden = NO;
        self.actionBtn.selected = YES;
        
    }else if (([state isEqualToString:@"10"] || [state isEqualToString:@"20"]) && ([serviceState  isEqualToString:@"生效中"] || [serviceState isEqualToString:@"未生效"])) {
        //显示设备发货按钮
        self.actionBtn.hidden = NO;
        self.actionBtn.selected = NO;
        
    }else if (([state isEqualToString:@"30"] || [state isEqualToString:@"40"] || [state isEqualToString:@"103"]) && ([serviceState  isEqualToString:@"已到期"] || [serviceState isEqualToString:@"已退款"] || [serviceState isEqualToString:@"生效中"]) && ![vipService isEqualToString:@"3"]){
        //其他类型不变
        self.actionBtn.hidden = NO;
        self.actionBtn.selected = YES;
        
    }else{
        self.actionBtn.hidden = YES;
    }
}

//商城的
-(void)setShopDic:(NSDictionary *)shopDic{
    _shopDic = shopDic;
    if (!shopDic || shopDic.allKeys.count == 0) return;
    self.nameTitle.text = @"商城购买";
    self.typeTitle.text = @"订        单";
    self.nameLabel.text = LGNSString(shopDic[@"bus_name"]);
    self.patientLabel.text = [NSString stringWithFormat:@"%@  %@",shopDic[@"patient_name"],shopDic[@"mobile"]];
    //state:0-待发货 、 2-已发货 、3-待回收 、9-回收失败 、101-已入库
    NSString *state = LGNSString(shopDic[@"ship_state"]);
    //0-待支付 、 10-支付中 、 100-已支付 、 101-已取消 、 102-支付超时
    NSString *payState = LGNSString(shopDic[@"pay_state"]);
    //0-未退款 、 10-退款中 、 90-部分退款 、 100-已退款 、 101-退款失败
    NSString *refundState = LGNSString(shopDic[@"refund_state"]);

    self.orderStatus.text = LGNSString(shopDic[@"refund_state_name"]);
    self.orderStatus.textColor = [refundState isEqualToString:@"0"]?[UIColor colorWithString:@"#4C87FD"]:[UIColor colorWithString:@"#909399"];
    
    self.deviceStatus.text = LGNSString(shopDic[@"ship_state_name"]);
    self.deviceStatus.textColor = [self.deviceColor.allKeys containsObject:state]?[UIColor colorWithString:self.deviceColor[state]]:[UIColor colorWithString:@"#909399"];
    
    //显示设备发货按钮
    if ([payState isEqualToString:@"100"] && [state isEqualToString:@"0"] && [refundState  isEqualToString:@"0"]) {
        self.actionBtn.hidden = NO;
        self.actionBtn.selected = NO;
        
    }else if ([refundState isEqualToString:@"100"] && ([state isEqualToString:@"2"] || [state isEqualToString:@"3"])){
        self.actionBtn.hidden = NO;
        self.actionBtn.selected = YES;
        
    }else{
        self.actionBtn.hidden = YES;
    }
}

-(void)actionBtnTouched{
    if (self.actionBtn.selected == NO) {
        //设备发货
        if (self.delegate && [self.delegate respondsToSelector:@selector(deliverDeviceAction:)]) {
            [self.delegate deliverDeviceAction:self.index];
        }
    }else{
        //设备回收
        if (self.delegate && [self.delegate respondsToSelector:@selector(recycleDeviceAction:)]) {
            [self.delegate recycleDeviceAction:self.index];
        }
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.type = LGPositionTypeSingle;
    [self.baseView addSubview:self.patientLabel];
    [self.baseView addSubview:self.actionBtn];
    [self.baseView addSubview:self.backView];
    [self.backView addSubview:self.nameTitle];
    [self.backView addSubview:self.nameLabel];
    [self.backView addSubview:self.typeTitle];
    [self.backView addSubview:self.orderStatus];
    [self.backView addSubview:self.deviceTitle];
    [self.backView addSubview:self.deviceStatus];
    [self.backView addSubview:self.detailBtn];
    
    [self.patientLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(16));
        make.centerY.equalTo(self.actionBtn);
    }];
    
    [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(16));
        make.top.equalTo(self.baseView).offset(viewPix(10));
        make.width.equalTo(@(viewPix(88)));
        make.height.equalTo(@(viewPix(30)));
    }];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(45));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.bottom.equalTo(self.baseView).offset(-viewPix(12));
    }];
    
    [self.nameTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView).offset(viewPix(10));
        make.left.equalTo(self.backView).offset(viewPix(12));
        make.width.equalTo(@(70));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameTitle.mas_right).offset(viewPix(12));
        make.right.equalTo(self.backView).offset(-viewPix(12));
        make.top.equalTo(self.nameTitle);
    }];
    
    [self.typeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameTitle.mas_bottom).offset(viewPix(15));
        make.left.equalTo(self.nameTitle);
    }];
    
    [self.orderStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.centerY.equalTo(self.typeTitle);
    }];
    
    [self.deviceTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeTitle.mas_bottom).offset(viewPix(15));
        make.bottom.equalTo(self.backView).offset(-viewPix(12));
        make.left.equalTo(self.typeTitle);
    }];
    
    [self.deviceStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.centerY.equalTo(self.deviceTitle);
    }];
    [self.detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backView).offset(-viewPix(8));
        make.centerY.equalTo(self.deviceTitle);
        make.width.equalTo(@(viewPix(50)));
        make.height.equalTo(@(viewPix(20)));
    }];
}

-(UILabel *)patientLabel{
    if(!_patientLabel){
        _patientLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _patientLabel;
}


-(UIView *)backView{
    if(!_backView){
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = [UIColor colorWithString:@"#F7F9FB"];
        _backView.cornerRidus = 5;
    }
    return _backView;
}

-(UILabel *)nameTitle{
    if(!_nameTitle){
        _nameTitle = [UILabel lableWithText:@"设备赠送" colorString:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _nameTitle;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:2];
    }
    return _nameLabel;
}

-(UILabel *)typeTitle{
    if(!_typeTitle){
        _typeTitle = [UILabel lableWithText:@"订      单" colorString:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _typeTitle;
}

-(UILabel *)orderStatus{
    if(!_orderStatus){
        _orderStatus = [UILabel lableWithText:@"" colorString:@"#4C87FD" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _orderStatus;
}

-(UILabel *)deviceTitle{
    if(!_deviceTitle){
        _deviceTitle = [UILabel lableWithText:@"设        备" colorString:@"#606266" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _deviceTitle;
}

-(UILabel *)deviceStatus{
    if(!_deviceStatus){
        _deviceStatus = [UILabel lableWithText:@"" colorString:@"#4C87FD" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _deviceStatus;
}

-(UIButton *)actionBtn{
    if(!_actionBtn){
        _actionBtn = [UIButton buttonWithTitle:@"设备发货" titleFont:LGFontWeight(14, UIFontWeightMedium) normalColor:@"#FFFFFF" selectColor:@"#4C87FD"];
        _actionBtn.cornerRidus = viewPix(16);
        [_actionBtn setTitle:@"设备回收" forState:UIControlStateSelected];
        [_actionBtn setBackgroundColor:@"#5E8EF5" selectColor:@"#FFFFFF"];
        [_actionBtn addBorder:@"#4C87FD" lineWidth:1 cornerRidus:viewPix(15)];
        [_actionBtn addTarget:self action:@selector(actionBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    }
    return _actionBtn;
}

-(UIButton *)detailBtn{
    if(!_detailBtn){
        _detailBtn = [UIButton buttonWithTitle:@"详情" titleFont:LGFont(16) textColor:@"#606266" imageName:@"grayTip16"];
        _detailBtn.bounds = CGRectMake(0, 0, viewPix(50), viewPix(20));
        _detailBtn.userInteractionEnabled = NO;
        [_detailBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:2];
    }
    return _detailBtn;
}

@end
